<?php
session_start();  
include('../Model/db.php');
include('../Model/News.php');

function getNewsById(){
	connectDB();
	$result = News::getNews($_GET['idNews']);
	disconnectDB();
	echo json_encode($result);
}

function getCountRow(){
	connectDB();
	$result = News::getCountRow();
	disconnectDB();
	echo json_encode($result);
}

function getNewsByLimit(){
	connectDB();
	$result = News::getNewsLimit($_GET['fromNews'], $_GET['toNews']);
	disconnectDB();
	echo json_encode($result);
}

function getAll(){
	if(isset($_SESSION['wkadmin'])){
		connectDB();
		$result = News::getAll();
		disconnectDB();
		echo json_encode($result);
	} else {
		echo json_encode(Array());
	}
}

function insertNews(){
	connectDB();
	autoCommitOff();
	$news_image = '';
	if (!isset($_FILES['image']) || $_FILES['image']["error"] > 0) {
	} else {
		$maxId = News::getMaxId();
		$maxId = $maxId['maxId'] + 1;
	
		$path = $_FILES['image']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		$_FILES['image']["name"]= $maxId.'.'.$ext;
		move_uploaded_file($_FILES['image']["tmp_name"], '../images/news/'.$_FILES['image']["name"]);
		$news_image	= "server/images/news/".$_FILES['image']["name"];
	}
	News::insertNews($_POST['title'], $_POST['summary'], $_POST['desc'], $_POST['date'], $_SESSION['wkadmin'], $news_image);
	$result = commitTRAN();
	disconnectDB();
	echo json_encode($result);
}

function updateNews(){	
	connectDB();
	autoCommitOff();
	if (!isset($_FILES['image']) || $_FILES['image']["error"] > 0) {
		News::updateNewsWithoutImage($_POST['id'], $_POST['title'], $_POST['summary'], $_POST['desc'], $_POST['date'], $_SESSION['wkadmin']);
	} else {
		$path = $_FILES['image']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		$_FILES['image']["name"]= $_POST['id'].'.'.$ext;
		move_uploaded_file($_FILES['image']["tmp_name"], '../images/news/'.$_FILES['image']["name"]);
		$news_image	= "server/images/news/".$_FILES['image']["name"];
		News::updateNews($_POST['id'], $_POST['title'], $_POST['summary'], $_POST['desc'], $_POST['date'], $_SESSION['wkadmin'], $news_image);
	}
	
	$result = commitTRAN();
	disconnectDB();
	echo json_encode($result);
}

function deleteNews(){
	connectDB();
	autoCommitOff();
	News::deleteNews($_POST['id']);
	$result = commitTRAN();
	disconnectDB();
	echo json_encode($result);
}

if($_POST){
	if(isset($_SESSION['wkadmin'])){
		if(isset($_POST['action'])){
			$action = $_POST['action'];
			switch($action){
				case 'insert' : insertNews(); break;
				case 'update' : updateNews(); break;
				case 'delete' : deleteNews(); break;
				default: echo json_encode(-1); break;
			}
		} else {
			echo json_encode(-1);
		}
	} else {
		echo json_encode(-1);
	}
}

if($_GET){
	if(isset($_GET['action'])){
		$action = $_GET['action'];
		switch($action){
			case 'getById' : getNewsById(); break;
			case 'getByLimit' : getNewsByLimit(); break;
			case 'getAll': getAll(); break;
			case 'getCountRow' : getCountRow(); break;
			default: echo json_encode(-1); break;
		}
	} else {
		echo json_encode(-1);
	}
}
?>